<template>
    <div id="home">
      <nav-bar class="home-nav">
        <div slot="center">购物街</div>
      </nav-bar>
      <tab-control  :titles="['流行','新款','精选']" class="tab-control" @tabClick="tabClick" ref="tabControl1" v-show="isTabFixed"/>
      <scroll class="content" ref="scroll"
              :probe-type="3"
              @scroll="contentScroll"
              :pull-up-load="true"
              @pullingUp="loadMore"
      >

        <home-swiper :foods="foods" @swiperImageLoad="swiperImageLoad"/>
        <tab-control  :titles="['流行','新款','精选']"  @tabClick="tabClick" ref="tabControl2" />
        <recommend-view :recommends="foods"/>
        <feature-view/>
        <goods-list :goods="showGoods"/>

      </scroll >
      <back-top @click.native="backClick" v-show="isShowBackTop" />
    </div>
</template>

<script>

  import HomeSwiper from './childComps/HomeSwiper';
  import  RecommendView from './childComps/RecommendView'
  import FeatureView from './childComps/FeatureView'

  import NavBar from '../../content/navbar/NavBar';
  import TabControl from '../../components/tabControl';
  import GoodsList from '../../components/goods/GoodsList';
  import Scroll from '../../content/scroll/Scroll';
  //import BackTop from '../../content/backTop/BackTop'

  import {getGoods,getHomeGoods,getHomeNew,getHomeSell} from "../../network/home"
  import {itemListenerMixin,backTopMixin} from "../../common/mixin"
  //import { debounce } from "common/utils";

  export default{
        name: "home",
      components:{
        NavBar,
       TabControl,
        HomeSwiper,
        RecommendView,
        FeatureView,
        GoodsList,
        Scroll
        //BackTop
      },
    data(){
          return {
            //result:null
            foods:[],
            goods:{
              'pop1':{ list:[]},
              'new':{ list:[]},
              'sell':{ list:[]}
            },
            currentType:'pop1',
            //isShowBackTop:false,
            index:0,
            tabOffsetTop:0,
            isTabFixed:false,
            saveY:0,
            itemImgListener:null
          }
    },
    mixins:[
      itemListenerMixin,
      backTopMixin
    ],
    computed:{
          showGoods(){
            return this.goods[this.currentType].list
          }
    },
    destroyed(){

    },
    //进入激活
    activated(){
         // this.$refs.scroll.scrollTo(0,this.saveY,0)
          this.$refs.scroll.refresh()
    },
    //离开不激活
    deactivated(){
          this.saveY = this.$refs.scroll.getScrollY()
      //.取消全局事件的监听
      this.$bus.$off('ItemImageLoad',this.itemImgListener)
    },
    created() {
          //1.请求多个数据
      this.getGoods()
      //2.请求商品数据
      this.getHomeGoods()
      this.getHomeNew()
      this.getHomeSell()

      //3.监听item中图片加载完成
     /*监听refresh应该在mounted*/
    },
    mounted(){
           this.tabClick(0)
          /*const refresh = debounce(this.$refs.scroll.refresh,10)
      this.itemImgListener = ()=>{
        refresh()
      }
      this.$bus.$on('ItemImageLoad',this.itemImgListener)*/

    },
    methods:{
          /*事件监听*/
      tabClick(index){
        switch (index) {
          case 0:
            this.currentType = 'pop1'
                break
          case 1:
            this.currentType = 'new'
            break
          case 2:
            this.currentType = 'sell'
            break
        }
        this.$refs.tabControl1.currentIndex = index;
        this.$refs.tabControl2.currentIndex = index;
      },
/*      backClick(){
        console.log('回到顶部');
        this.$refs.scroll.scrollTo(0,0);
        this.$refs.scroll.message
      },*/
      contentScroll(position){
        /*console.log(position1);*/
        /*this.isShowBackTop=-(position.y)>1000*/
        this.listenShowBackTop(position) //在混入里有抽离的函数
        //.决定tabControl是否吸顶
        this.isTabFixed = (-position.y)>this.tabOffsetTop
      },
/*      listenShowBackTop(position){
        this.isShowBackTop=-(position.y)>1000
      },*/
      loadMore(){
        window.console.log('上拉加载更多');
        this.getHomeGoods()
        this.$refs.scroll.refresh()
      },
      swiperImageLoad(){
        this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
      },
          /*网络请求*/
          getHomeGoods(){
            getHomeGoods().then(res=>{
              const index = this.index + 1
              console.log(res.data[0].pop1[index-1].list)
              this.goods['pop1'].list.push(...res.data[0].pop1[index-1].list)
              this.index +=1

              this.$refs.scroll.finishPullUp()
            })
          },
      getHomeNew(){
        getHomeNew().then(res=>{

          console.log(res.data[0].new[0].list)
          this.goods['new'].list.push(...res.data[0].new[0].list)

        })
      },
      getHomeSell(){

        getHomeSell().then(res=>{
          this.goods['sell'].list.push(...res.data[0].sell[0].list)
          console.log(res.data[0].sell[0].list)

        })
      },
      getGoods(){

      getGoods().then(res =>{
          this.foods = res.data[0].foods

        })
      }
    }
  }
</script>

<style scoped>
  .home-nav{
    background-color: var(--color-tint);
    color: white;
    top: 0;
/*    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 2;*/
  }

  #home{height: 100vh;position: relative;padding-top: 0;margin-top: 0;border: 0}
  .content{
    overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
  .tab-control{position: relative;z-index: 9}
/*  .content{
    height: calc(100% - 98px);
    overflow: hidden;
    margin-top: 44px;
  }*/
</style>
